package cn.ac.iie.di.ban.data.exchange.runner.slave;

import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.ScopeEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.StatusEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.TaskTypeEnum;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.task.order.TaskOrderRequest;
import cn.ac.iie.di.ban.data.exchange.runner.commons.protocol.task.order.TaskOrderResponse;
import cn.ac.iie.di.ban.data.exchange.runner.commons.worker.SlaveWorker;
import cn.ac.iie.di.ban.data.exchange.runner.slave.task.checkTask.CheckSQLAndPermissionTask;
import cn.ac.iie.di.ban.data.exchange.runner.slave.task.runTask.QueryTask;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by Black-Laptop on 12/6/2017.
 */
public class QuerySlave implements SlaveWorker {

    public static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(QuerySlave.class);

    private TaskTypeEnum type;
    private ScopeEnum scope;

    private QueryTask queryTask;

    private TaskOrderRequest taskOrderRequest;
    private String uniq_key;

    public static FinishedHook callAfterFinished;

    private String dbHosts;
    private String dbUser;
    private String dbPassword;
    private String dbName;
    private String ftpIp;
    private int ftpPort;
    private String ftpUser;
    private String ftpPassword;

    /**
     * TaskTypeEnum.CHECK 功能时不需要穿任何参数
     */
    public QuerySlave() {
    }

    /**
     *
     * @param dbHosts dbip:dbport 数据库ip地址：数据库端口号
     * @param dbUser 数据库用户名
     * @param dbPassword 数据库密码
     * @param dbName     数据库的名字
     */
    public QuerySlave(String dbHosts, String dbUser, String dbPassword, String dbName) {
        this.dbHosts = dbHosts;
        this.dbUser = dbUser;
        this.dbPassword = dbPassword;
        this.dbName = dbName;

    }


    @Override
    public String getName() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public TaskOrderResponse execute(TaskOrderRequest taskOrderRequest, FinishedHook callAfterFinished) {
        this.type = taskOrderRequest.getType();
        this.scope = taskOrderRequest.getScope();
        QuerySlave.callAfterFinished = callAfterFinished;

        this.taskOrderRequest = taskOrderRequest;
        this.uniq_key = taskOrderRequest.getUniq_key();

        TaskOrderResponse taskOrderResponse = null;

        try {
            taskOrderResponse = start();
            return taskOrderResponse;
        } catch (Exception ex) {
            taskOrderResponse = new TaskOrderResponse(uniq_key, StatusEnum.QUERY_TASK_ERROR, ex.getMessage());
        }

        return taskOrderResponse;
    }

    @Override
    public boolean finished() {
        return getQueryTaskThreadStatus();
    }

    @Override
    public void stop() {
        stopQueryTaskThread();
    }

    private TaskOrderResponse start() throws Exception {

        TaskOrderResponse taskOrderResponse = null;

        switch (this.type) {
            case CHECK: {
                CheckSQLAndPermissionTask checkSQLAndPermissionTask = new CheckSQLAndPermissionTask(this.taskOrderRequest);
                try {
                    taskOrderResponse = checkSQLAndPermissionTask.checkSQLAndPermission(uniq_key);
                } catch (Exception e) {
                    taskOrderResponse.setDetail("checkSQLAndPermission error");
                }
                break;
            }
            case RUN: {
                taskOrderResponse = runTask();
                break;
            }
            default:
                break;
        }

        return taskOrderResponse;
    }

    private Boolean getQueryTaskThreadStatus() {
        if (this.queryTask != null) {
            return queryTask.getQueryTaskThreadStatus();
        }

        return false;
    }

    private void stopQueryTaskThread() {
        if (this.queryTask != null) {
            queryTask.stopQueryTaskThread();
        }
    }


    private TaskOrderResponse runTask() throws Exception {

        CheckSQLAndPermissionTask checkSQLAndPermissionTask = new CheckSQLAndPermissionTask(this.taskOrderRequest);
        checkSQLAndPermissionTask.checkSQLAndPermission(uniq_key);

        TaskOrderResponse taskOrderResponse = null;
        Boolean doQuery = ScopeEnum.SEARCH.equals(scope);
        if (doQuery) {
            this.queryTask = new QueryTask(this.taskOrderRequest, dbHosts, dbUser, dbPassword, dbName);
            taskOrderResponse = queryTask.doQueryTask();
            return taskOrderResponse;
        }
        return taskOrderResponse;
    }


}
